package GUI;

import 学生.Student;
import 老师.teacher;
import 数据库和网络.myDatebase;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class registerWindow extends JFrame {
    String preID,prePassword;
    JTextField inputID,inputPassword;

    public registerWindow(){
        if(enterWindow.stuOption.isSelected()){
            enterWindow.identity="学生";
        }else
        if(enterWindow.teaOption.isSelected()){
            enterWindow.identity="老师";
        }else{
            incorrectRegWindow incorrectRegWindow= new incorrectRegWindow();
            return;
        }
        JPanel panel2 = new JPanel(new GridLayout(5, 2,20,10));
        JButton goRegister= new JButton("注册");
        JLabel lab1=new JLabel(enterWindow.identity+"账号注册： ");
        JLabel lab2=new JLabel(" ");
        JLabel lab3=new JLabel("账号：");
        JLabel lab4=new JLabel("密码：");
        JLabel lab5=new JLabel(" ");
        JLabel lab6=new JLabel(" ");
        JButton goBack = new JButton("返回");
        inputID = new JTextField(10);
        inputPassword = new JTextField(10);
        lab3.setHorizontalAlignment(SwingConstants.CENTER);
        lab4.setHorizontalAlignment(SwingConstants.CENTER);
        this.setSize(320,270);
        panel2.setSize(this.getSize());
        panel2.add(lab5);
        panel2.add(lab6);
        panel2.add(lab1);
        panel2.add(lab2);
        panel2.add(lab3);
        panel2.add(inputID);
        panel2.add(lab4);
        panel2.add(inputPassword);
        panel2.add(goBack);
        panel2.add(goRegister);
        inputID.requestFocus();
        goRegister.setBackground(Color.lightGray);
        goBack.setBackground(Color.lightGray);
        goBack.addActionListener(new backListener());
        goRegister.addActionListener(new LogListener2());
        this.getContentPane().add(BorderLayout.CENTER,panel2);
        this.setLayout(new FlowLayout());//流式布局
        this.setLocationRelativeTo(null);//居中
        this.setTitle("在线考试平台");
        this.setVisible(true);
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    public class LogListener2 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            preID=inputID.getText();
            prePassword=inputPassword.getText();
                if(enterWindow.stuOption.isSelected()){
                    Student.studentRegister(preID,prePassword);
                    registerWindow.this.dispose();
                }
                else if(enterWindow.teaOption.isSelected()){
                    teacher.teacherRegister(preID,prePassword);
                    registerWindow.this.dispose();
                }
        }
    }


    public class backListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            registerWindow.this.dispose();
            new enterWindow();
        }
    }
}
